package top.forgirl.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import top.forgirl.common.group.Delete;
import top.forgirl.common.group.Insert;
import top.forgirl.common.group.Search;
import top.forgirl.common.result.Result;
import top.forgirl.common.result.ResultType;
import top.forgirl.domain.Apply;
import top.forgirl.domain.dto.ApplyDo;
import top.forgirl.service.ApplyService;

import java.util.List;

@Api(tags = "申请记录模块")
@RestController
public class ApplyController {

    @Autowired
    private ApplyService applyService;

    @ApiOperation("查询申请记录")
    @PostMapping("/apply/list/{offset}/{count}")
    public Result<List<Apply>> listApply(@RequestBody @Validated({Search.class}) ApplyDo applyDo,
                                         @PathVariable int offset,
                                         @PathVariable int count) {

        List<Apply> applyList = applyService.list(applyDo, offset, count);

        return new Result<>(applyList);

    }

    @ApiOperation("增加申请记录")
    @ApiOperationSupport(ignoreParameters = {"id","beginTime","endTime","dname"})
    @PostMapping("/apply/add")
    public Result addApply(@RequestBody @Validated({Insert.class}) ApplyDo applyDo) {
        Apply apply = applyService.insert(applyDo);
        if (apply != null) {
            return new Result(apply);
        } else {
            return new Result(ResultType.FAILED, "插入失败");
        }
    }

    @ApiOperation("删除申请记录")
    @ApiOperationSupport(includeParameters = {"id"})
    @DeleteMapping("/apply/delete")
    public Result deleteApply(@RequestBody @Validated({Delete.class}) ApplyDo applyDo) {

        Apply apply = applyService.delete(applyDo);
        if (apply != null) {
            return new Result(apply);
        }else{
            return new Result(ResultType.FAILED,"删除失败");
        }

    }

}
